-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/31
-- @module:   common
-- @describe: 药品
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class common
local common = {
    WEMIX_PATH = "夜鸦:自动铸币:",
    WEMIX_PATH_REQ = "夜鸦:自动铸币:申请"
}

local this = common
-- 保存延时读取
local is_sleep_any_t = {}
-- 保存指定KEY到表
local set_any_table = {}
-- 保存延迟启动指定函数列表
local cache_read = {}
-------------------------------------------------------------------------------------
-- 指定任务 在指定时间间隔内是否可执行
------------------------------------------------------------------------------------
common.is_sleep_any = function(arg, time)
    local args = arg or '全局'
    time = time or 1
    if is_sleep_any_t[args] ~= nil then
        if os.time() - is_sleep_any_t[args] < time then
            return false
        end
    end
    is_sleep_any_t[args] = os.time()
    return true
end


-------------------------------------------------------------------------------------
-- 根据角度，距离，坐标 获取坐标
common.get_circle_pos = function(x, y, n_angle, dis)
    if not n_angle then
        return x, y
    end
    local n_sin = math.sin(math.rad(n_angle))
    local n_cos = math.cos(math.rad(n_angle))
    local fx = x + n_cos * dis
    local fy = y + n_sin * dis
    return tonumber(string.format('%0.1f', fx)), tonumber(string.format('%0.1f', fy))
end

-------------------------------------------------------------------------------------
-- 分割字符中的数据:格式 如：'开启交易转金:0，交易后下线:0，转金保留金币:0，触发交易金:50'
-- 分割后为 0,0,0,50
common.split_to_unpack = function(value2)
    local tab = {}
    for k in string.gmatch(value2, '-?%d+') do
        table.insert(tab, k)
    end
    return table.unpack(tab)
end

------------------------------------------------------------------------------------
-- 延迟读取指定函数的返回值[单参返回]
------------------------------------------------------------------------------------
common.get_cache_result = function(name, func, time, ...)
    if cache_read[name] then
        if os.time() - cache_read[name].time < time then
            return cache_read[name].result
        end
    end
    local result = func(...)
    cache_read[name] = { result = result, time = os.time() }
    return result
end

------------------------------------------------------------------------------------
-- 读取指定KEY的VALUE
common.get_key_table = function(key)
    return set_any_table[key] or false
end

------------------------------------------------------------------------------------
-- 设置指定KEY = VALUE到表
common.set_key_table = function(key, value)
    set_any_table[key] = value
end


------------------------------------------------------------------------------------
-- 两点之间距离
function this.get_dis_by_pos(pos1, pos2)
    local val = (pos2.x - pos1.x) ^ 2 + (pos2.y - pos1.y) ^ 2 + (pos2.z - pos1.z) ^ 2
    local ret = (val) ^ (1 / 2)
    return ret
end

------------------------------------------------------------------------------------
-- [条件] 判断指定名字是否在列表存在
--
-- @tparam          any                                  list              物品列表，物品名或其他字段如{'A','B','C'},{1,2,3}
-- @tparam          any                                  name              需要在list中配对的目标参数[可string/number]
-- @tparam          bool                                 stop_vague        是否开启精准匹配
-- @treturn         bool                                                   返回 true (name 在 list 存在)
-- @usage
-- local is_exist = item_ent.is_exist_list_arg('A',A)
-- local is_exist = item_ent.is_exist_list_arg({'A','B'},A)
-- local is_exist = item_ent.is_exist_list_arg(12,1)
-- local is_exist = item_ent.is_exist_list_arg({12,13},1)
------------------------------------------------------------------------------------
this.is_exist_list_arg = function(list, name, stop_vague)
    local t = type(list)
    if t ~= 'nil' and t ~= 'table' then
        list = { list }
    end
    if type(list) == 'table' then
        for _, v in pairs(list) do
            if name ~= '' and v ~= ''
                and (v == name or type(name) == 'string' and not stop_vague
                    and string.find(name, v)) or name == v and v == '' then
                return true
            end
        end
    end
    return false
end
------------------------------------------------------------------------------------
-- [条件] 判断指定名字是否在列表存在
--
-- @tparam          any                                  list              物品列表，物品名或其他字段如{'A','B','C'},{1,2,3}
-- @tparam          any                                  name              需要在list中配对的目标参数[可string/number]
-- @tparam          bool                                 stop_vague        是否开启精准匹配
-- @treturn         bool                                                   返回 true (name 在 list 存在)
-- @usage
-- local is_exist = item_ent.is_exist_list_arg('A',A)
-- local is_exist = item_ent.is_exist_list_arg({'A','B'},A)
-- local is_exist = item_ent.is_exist_list_arg(12,1)
-- local is_exist = item_ent.is_exist_list_arg({12,13},1)
------------------------------------------------------------------------------------
this.is_exist_list_name = function(list, name, stop_vague)
    local t = type(list)
    if t ~= 'nil' and t ~= 'table' then
        list = { list }
    end
    if type(list) == 'table' then
        for _, v in pairs(list) do
            local targetName = v.name
            if name ~= '' and targetName ~= ''
                and (targetName == name or type(name) == 'string' and not stop_vague
                    and string.find(name, targetName)) or name == targetName and targetName == '' then
                return true
            end
        end
    end
    return false
end


------------------------------------------------------------------------------------
--- 反转表
--- @tparam          table                                originalTable    需要反转的表
--- @treturn         table                                                   返回反转后的表
--- @usage
--- local new_table = item_ent.reverseTable({1,2,3})
--- print(new_table)
--- @see common.reverseTable
---------------------------------------------------------------------------------------
function this.reverseTable(originalTable)
    if table.is_empty(originalTable) then
        return {}
    end
    local reversedTable = {}
    for i = #originalTable, 1, -1 do
        table.insert(reversedTable, originalTable[i])
    end
    return reversedTable
end

---------------------------------------------------------------------------------------
--- 获取目标时间的时间戳
--- @tparam          number                                year            目标时间的年
--- @tparam          number                                month           目标时间的月
--- @tparam          number                                day            目标时间的日
--- @tparam          number                                hour           目标时间的时
--- @tparam          number                                min           目标时间的分
--- @tparam          number                                sec           目标时间的秒
--- @treturn         number                                                   目标时间戳
--- @usage
--- local getTargetTimeStamp = item_ent.timeDifference(2019, 12, 31, 23, 59, 59)
--- print(getTargetTimeStamp)
--- @see common.getTargetTimeStamp
---------------------------------------------------------------------------------------
function common.getTargetTimeStamp(year, month, day, hour, min, sec)
    -- 构造目标时间的日期表
    local target_date = {
        year = year,
        month = month,
        day = day,
        hour = hour,
        min = min,
        sec = sec
    }
    -- 获取目标时间的时间戳
    local target_timestamp = os.time(target_date)

    return target_timestamp
end

---------------------------------------------------------------------------------------
--- 计算时间差
--- @tparam          number                                year            目标时间的年
--- @tparam          number                                month           目标时间的月
--- @tparam          number                                day            目标时间的日
--- @tparam          number                                hour           目标时间的时
--- @tparam          number                                min           目标时间的分
--- @tparam          number                                sec           目标时间的秒
--- @treturn         number                                                   返回时间差
--- @treturn         number                                                   当前时间戳
--- @treturn         number                                                   目标时间戳
--- @usage
--- local time_difference = item_ent.timeDifference(2019, 12, 31, 23, 59, 59)
--- print(time_difference)
--- @see common.timeDifference
---------------------------------------------------------------------------------------
function common.getTimeDifference(year, month, day, hour, min, sec)
    -- 获取当前时间的日期表
    local current_date = os.date('*t')
    -- 获取当前时间的时间戳
    local current_timestamp = os.time(current_date)

    -- 构造目标时间的日期表
    local target_date = {
        year = year,
        month = month,
        day = day,
        hour = hour,
        min = min,
        sec = sec
    }
    -- 获取目标时间的时间戳
    local target_timestamp = os.time(target_date)

    -- 计算时间差
    local difference = target_timestamp - current_timestamp

    -- 返回时间差
    return difference, current_timestamp, target_timestamp
end

------------------------------------------------------------------------------------
-- [条件] 判断指定名字是否在列表存在
--
-- @tparam          any                                  list              物品列表，物品名或其他字段如{'A','B','C'},{1,2,3}
-- @tparam          any                                  name              需要在list中配对的目标参数[可string/number]
-- @tparam          bool                                 stop_vague        是否开启精准匹配
-- @treturn         bool                                                   返回 true (name 在 list 存在)
-- @usage
-- local is_exist = item_ent.is_exist_list_arg('A',A)
-- local is_exist = item_ent.is_exist_list_arg({'A','B'},A)
-- local is_exist = item_ent.is_exist_list_arg(12,1)
-- local is_exist = item_ent.is_exist_list_arg({12,13},1)
------------------------------------------------------------------------------------
this.is_exist_time_list_arg = function(list, name, stop_vague)
    local t = type(list)
    if t ~= 'nil' and t ~= 'table' then
        return false
    end

    if type(list) == 'table' then
        for _, v in ipairs(list) do
            local tmpName = v.name
            local year = v.year
            local month = v.month
            local day = v.day
            local hour = v.hour or 0
            local min = v.min or 0
            local sec = v.sec or 0
            local differenceTime = this.getTimeDifference(year, month, day, hour, min, sec)
            if differenceTime > 0 then
                if name ~= '' and tmpName ~= ''
                    and (tmpName == name or type(name) == 'string' and not stop_vague
                        and string.find(name, tmpName)) or name == tmpName and tmpName == '' then
                    return true
                end
            end
        end
    end
    return false
end

return this
